package com.kila.test;

import com.kila.entity.Student;
import com.kila.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.util.List;

import java.io.Reader;


public class test {

    //带转换器的增加学生

    public static void addStudentWithConverter(){
        Student stu = new Student();
        stu.setStuNo(3);
        stu.setStuName("王五");
        stu.setStuAge(17);
        stu.setGraName("3band");
        stu.setSex(true);

        SqlSession session=null;

        try {
            //加载MyBatis连接配置文件
            Reader reader= Resources.getResourceAsReader("config.xml");
            //通过SqlSessionFactoryBuilder.build()方法获得对象
            SqlSessionFactory sessionFactory =  new SqlSessionFactoryBuilder().build(reader);

            //获取session
            session = sessionFactory.openSession();
/**
 *

 session.insert("com.kila.entity.studentMapper."+"addStudent",stu);
 //新增加数据需要提交事务
 session.commit();
 */

            StudentMapper studentMapper  =  session.getMapper(StudentMapper.class);
             studentMapper.addStudentWithConverter(stu);
            session.commit();

            System.out.println("增加了数据");



        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();

        }


    }






  //使用了转换器查询学生
    public static void queryStudentByStunnoWithConverter(){

        SqlSession session=null;

        try {
            //加载MyBatis连接配置文件
            Reader reader= Resources.getResourceAsReader("config.xml");
            //通过SqlSessionFactoryBuilder.build()方法获得对象
            SqlSessionFactory sessionFactory =  new SqlSessionFactoryBuilder().build(reader);

            //获取session
            session = sessionFactory.openSession();
/**
 *
 Student s =(Student) session.selectOne("com.kila.entity.studentMapper.queryStudentByStuno",1);
 */
            StudentMapper  studentMapper =  session.getMapper(StudentMapper.class);
            Student s = studentMapper.queryStudnetByStunoWithConverter(2);
            System.out.println(s);



        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();

        }
    }









    //查询单个学生

    public static void queryStudent(){

        SqlSession session=null;

        try {
            //加载MyBatis连接配置文件
            Reader reader= Resources.getResourceAsReader("config.xml");
            //通过SqlSessionFactoryBuilder.build()方法获得对象
            SqlSessionFactory sessionFactory =  new SqlSessionFactoryBuilder().build(reader);

            //获取session
            session = sessionFactory.openSession();
/**
 *
            Student s =(Student) session.selectOne("com.kila.entity.studentMapper.queryStudentByStuno",1);
*/
            StudentMapper  studentMapper =  session.getMapper(StudentMapper.class);
            Student s = studentMapper.queryStudentByStuno(2);
            System.out.println(s);



        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();

        }
    }

    //查询全部学生
    public static void queryAllStudents(){
        SqlSession session=null;

        try {
            //加载MyBatis连接配置文件
            Reader reader= Resources.getResourceAsReader("config.xml");
            //通过SqlSessionFactoryBuilder.build()方法获得对象
            SqlSessionFactory sessionFactory =  new SqlSessionFactoryBuilder().build(reader);

            //获取session
            session = sessionFactory.openSession();
          /**
            List<Student> students = session.selectList("com.kila.entity.studentMapper.queryAllStudents");
          */

          StudentMapper stumap =session.getMapper(StudentMapper.class);
          List<Student> students =    stumap.queryAllStudents();

          System.out.println(students);



        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();

        }



    }

   //增加一个学生
     public static void addStudent(){
        Student stu = new Student();
        stu.setStuNo(3);
        stu.setStuName("王五");
        stu.setStuAge(17);
        stu.setGraName("3band");

        SqlSession session=null;

         try {
             //加载MyBatis连接配置文件
             Reader reader= Resources.getResourceAsReader("config.xml");
             //通过SqlSessionFactoryBuilder.build()方法获得对象
             SqlSessionFactory sessionFactory =  new SqlSessionFactoryBuilder().build(reader);

             //获取session
             session = sessionFactory.openSession();
/**
 *

             session.insert("com.kila.entity.studentMapper."+"addStudent",stu);
            //新增加数据需要提交事务
             session.commit();
 */

             StudentMapper studentMapper  =  session.getMapper(StudentMapper.class);
             int count =  studentMapper.addStudent(stu);
             session.commit();

             System.out.println("增加了"+count+"数据");



         }catch (Exception e){
             e.printStackTrace();
         }finally {
             session.close();

         }


     }


     //删除学生

    public static void deleteStudentByStuno(){


        SqlSession session=null;

        try {
            //加载MyBatis连接配置文件
            Reader reader= Resources.getResourceAsReader("config.xml");
            //通过SqlSessionFactoryBuilder.build()方法获得对象
            SqlSessionFactory sessionFactory =  new SqlSessionFactoryBuilder().build(reader);

            //获取session
            session = sessionFactory.openSession();
/**
 *

           int count = session.delete("com.kila.entity.studentMapper."+"deleteStudentBystuno",3);
 */

            StudentMapper studentMapper = session.getMapper(StudentMapper.class);
           int count = studentMapper.deleteStudentBystuno(3);
            //新增加数据需要提交事务
            session.commit();
            System.out.println("删除了"+count+"行数据");



        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();

        }


    }

     //修改学生
    public static void updateStudentByStuno(){


        SqlSession session=null;
        Student stu  = new Student(2,"zl",19,"g3");

        try {
            //加载MyBatis连接配置文件
            Reader reader= Resources.getResourceAsReader("config.xml");
            //通过SqlSessionFactoryBuilder.build()方法获得对象
            SqlSessionFactory sessionFactory =  new SqlSessionFactoryBuilder().build(reader);

            //获取session
            session = sessionFactory.openSession();
/**
 *

            int count = session.update("com.kila.entity.studentMapper."+"updateStudentByno",stu);

 */

             StudentMapper studentMapper = session.getMapper(StudentMapper.class);
            int count = studentMapper.updateStudentByno(stu);
            //新增加数据需要提交事务
            session.commit();
            System.out.println("修改了"+count+"行数据");



        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();

        }



    }




    public static void main(String[] args){
       //  addStudent();
       // queryAllStudent();
        // deleteStudentByStuno();
        // queryAllStudent();
       //  updateStudentByStuno();
         //queryAllStudent();
        //queryStudent();
//        addStudent();
//        queryAllStudents();
//        deleteStudentByStuno();
//        queryAllStudents();
//        updateStudentByStuno();
//        queryAllStudents();

//          使用转换器查询学生

        addStudentWithConverter();
        queryStudentByStunnoWithConverter();
        queryAllStudents();

    }
}
